﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;

namespace Lilium.Unity.Adapters
{
	public sealed class ToServiceProvider: IServiceProvider
	{
		public ToServiceProvider(IUnityContainer unityContainer)
		{
			if (unityContainer == null) throw new ArgumentNullException("unityContainer");

			m_UnityContainer = unityContainer;
		}

		#region IServiceProvider Members

		public object GetService(Type serviceType)
		{
			try
			{
				return m_UnityContainer.Resolve(serviceType);
			}
			catch (ResolutionFailedException)
			{	//TODO: log resolution exception
				return null; 
			}
		}

		public TService GetService<TService>()
		{
			try
			{
				return m_UnityContainer.Resolve<TService>();
			}
			catch (ResolutionFailedException)
			{   //TODO: log resolution exception
				return default(TService);
			}
		}

		#endregion

		#region private IUnityContainer UnityContainer

		private readonly IUnityContainer m_UnityContainer;

		private IUnityContainer UnityContainer
		{
			get
			{
				return m_UnityContainer;
			}
		}

		#endregion
	}
}
